﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DBUtility;
using CommonFunc;

public partial class Control_INewsList_Small : System.Web.UI.UserControl
{
    MODEL.INewsClass MINC = new MODEL.INewsClass();
    MODEL.INewsClass _MINC = new MODEL.INewsClass();

    BLL.INewsClass BINC = new BLL.INewsClass();

    #region 封装字段
    private int topNum;
    private int classID;
    private string filterExpression;
    private int subStrNum;
    private string dataListStyle;
    private int repeatColumn;
    private bool isIndex;

    public int TopNum
    {
        get { return topNum; }
        set { topNum = value; }
    }

    public int ClassID
    {
        get { return classID; }
        set { classID = value; }
    }

    public string FilterExpression
    {
        get { return filterExpression; }
        set { filterExpression = value; }
    }

    public int SubStrNum
    {
        get { return subStrNum; }
        set { subStrNum = value; }
    }

    public string DataListStyle
    {
        get { return dataListStyle; }
        set { dataListStyle = value; }
    }

    public int RepeatColumn
    {
        get { return repeatColumn; }
        set { repeatColumn = value; }
    }

    public bool IsIndex
    {
        get { return isIndex; }
        set { isIndex = value; }
    }
    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
        ControlDataInit();

        DLStyleInit();
    }


    #region ControlDataInit
    protected void ControlDataInit()
    {
        try
        {
            string SQLstr_INews;

            if (this.TopNum != 0)
            {
                SQLstr_INews = "SELECT TOP " + this.TopNum + " * FROM [INews] WHERE [IN_IsLock]=false AND [IN_IsDel]=false ";
            }
            else
            {
                SQLstr_INews = "SELECT * FROM [INews] WHERE [IN_IsLock]=false AND [IN_IsDel]=false ";
            }

            if (this.IsIndex == true)
            {
                SQLstr_INews += " AND [IN_Index]=true ";
            }

            if (this.ClassID != 0)
            {
                MINC.INC_ID = this.ClassID;

                _MINC = BINC.GetDataByID(MINC);

                string ClassCode = _MINC.INC_ClassCode;

                SQLstr_INews += "AND [IN_ClassCode] LIKE '" + ClassCode + "%' ";
            }

            if (this.FilterExpression != null)
            {
                SQLstr_INews += "AND " + this.FilterExpression;
            }

            SQLstr_INews += "ORDER BY [IN_Order] DESC";

            DataTable DT_INews = AccHelper.GetDataTable(SQLstr_INews);

            DL_INewsList.DataSource = DT_INews.DefaultView;

            DL_INewsList.DataBind();
        }
        catch (Exception ex)
        {
            LB_INewsList_Small.Text = ex.Message;
        }
    }
    #endregion


    #region DLStyleInit
    protected void DLStyleInit()
    {
        try
        {
            switch (this.DataListStyle)
            {
                case "Horizontal":
                    DL_INewsList.RepeatDirection = RepeatDirection.Horizontal;
                    break;
                case "Vertical":
                    DL_INewsList.RepeatDirection = RepeatDirection.Vertical;
                    break;
            }


            DL_INewsList.RepeatColumns = this.RepeatColumn;
        }
        catch (Exception ex)
        {
            LB_INewsList_Small.Text = ex.Message;
        }
    }
    #endregion


    #region 格式化文章标题
    protected string FormatTitle(object _Title)
    {
        string Title = Convert.ToString(_Title);

        if (this.SubStrNum == 0)
        {
            return Title;
        }
        else
        {
            if (Title.Length > this.SubStrNum)
            {
                return CommonFunction.SubStr(Title, this.SubStrNum, " …");
            }
            else
            {
                return Title;
            }
        }
    }
    #endregion
}
